package com.ruoyi.excelImport.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.excelImport.mapper.BusTestMapper;
import com.ruoyi.excelImport.domain.BusTest;
import com.ruoyi.excelImport.service.IBusTestService;

/**
 * 测试导入模块Service业务层处理
 *
 * @author chentao
 * @date 2022-09-04
 */
@Service
public class BusTestServiceImpl extends ServiceImpl<BusTestMapper, BusTest> implements IBusTestService
{


    /**
     * 查询测试导入模块列表
     *
     *
     * @param busTest 测试导入模块
     * @return 测试导入模块
     */
    @Override
    public List<BusTest> selectBusTestList(BusTest busTest)
    {
        return baseMapper.selectBusTestList(busTest);
    }

    @Override
    public String importData(List<BusTest> busTests, Boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(busTests) || busTests.size() == 0)
        {
            throw new ServiceException("导入测试数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BusTest busTest : busTests)
        {
            try
            {
                QueryWrapper<BusTest> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", busTest.getName());
                // 验证是否存在这个数据
                BusTest u = baseMapper.selectOne(queryWrapper);
                if (StringUtils.isNull(u))
                {
                    busTest.setCreateBy(operName);
                    baseMapper.insert(busTest);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + busTest.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    busTest.setId(u.getId());
                    busTest.setUpdateBy(operName);
                    baseMapper.updateById(busTest);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + busTest.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + busTest.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + busTest.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
